package com.hgkj.cloud.customer.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class QuestionnaireExcelParseTool {
    private String mFilePath;
    public List<QuestionnaireExcel> qns = new ArrayList<QuestionnaireExcel>();
    public boolean invalid_method = false;  //excel第一行不对

    public QuestionnaireExcelParseTool() {
    }

    public void setFilePath(String filePath) {
        mFilePath = filePath;
    }

    private static final String SUFFIX_2003 = ".xls";
    private static final String SUFFIX_2007 = ".xlsx";

    public Workbook initWorkBook() throws IOException {

        File file = new File(mFilePath);
        InputStream is = new FileInputStream(file);

        Workbook workbook = null;
        try {
            if (mFilePath.endsWith(SUFFIX_2003)) {
                workbook = new HSSFWorkbook(is);
            } else if (mFilePath.endsWith(SUFFIX_2007)) {
                workbook = new XSSFWorkbook(is);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }

        return workbook;
    }

    public void parseWorkbook(Workbook workbook) {
        int numOfSheet = workbook.getNumberOfSheets();

        for (int i = 0; i < numOfSheet; ++i) {
            Sheet sheet = workbook.getSheetAt(i);
            parseSheet(sheet);
        }
    }

    private void parseSheet(Sheet sheet) {
        Row row;

        int count = 0;

        Iterator<Row> iterator = sheet.iterator();
        while (iterator.hasNext()) {
            row = iterator.next();

            if (count == 0) {
                parseRowAndFindMethod(row);
            } else {
                parseRowAndFillData(row, count);
            }

            ++count;
        }
    }

    private void parseRowAndFindMethod(Row row) {
        List<String> rst = parseRow(row);
        for (int i = 0; i < rst.size(); i++) {
            if (i == 0 && !"题目".equals(rst.get(i))) {
                this.invalid_method = true;
            }
            if (i > 0) {
                if (i % 2 != 0) {
                    if (!rst.get(i).startsWith("选项")) {
                        this.invalid_method = true;
                    }
                } else {
                    if (!rst.get(i).startsWith("分数")) {
                        this.invalid_method = true;
                    }
                }
            }
        }

    }

    private void parseRowAndFillData(Row row, int count) {
        List<String> rst = parseRow(row);

        try {
            QuestionnaireExcel qnexcel = new QuestionnaireExcel();
            for (int i = 0; i < rst.size(); i++) {
                if (rst.get(0) != null && !"".equals(rst.get(0))) {
                    if (i == 0)
                        qnexcel.setQuestion(rst.get(i));
                    else {
                        if (rst.get(i) != null && !"".equals(rst.get(i))) {
                            AnswerExcel answer = new AnswerExcel();
                            answer.setSort_code(i);
                            if (i % 2 != 0) {
                                answer.setAnswer(rst.get(i));
                                i++;
                                answer.setScore(Integer.valueOf(rst.get(i)==null ? "0":rst.get(i)));
                                qnexcel.getAnswers().add(answer);
                            }
                        }
                    }
                }
            }

            if (rst.get(0) != null && !"".equals(rst.get(0)))
                qns.add(qnexcel);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    private List<String> parseRow(Row row) {
        List<String> rst = new ArrayList<>();
        Cell cell;
        Iterator<Cell> iterator = row.iterator();
        while (iterator.hasNext()) {
            cell = iterator.next();
            cell.setCellType(CellType.STRING);
            rst.add(cell.getStringCellValue());
        }
        return rst;
    }
}
